package com.litchishell.ssmdemo.config;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

/**
 * @author LitchiShell
 * @description:
 * @create 2022-04-17
 */
@Configuration
public class DataSourceConfig {

    @Bean("dataSource")
    public DriverManagerDataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        // 设置数据库的驱动类名
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        // 设置数据库URL
        dataSource.setUrl("jdbc:mysql://localhost:3306/spring");
        // 设置数据库的用户名
        dataSource.setUsername("root");
        // 设置数据库的密码
        dataSource.setPassword("root");
        return dataSource;
    }

    @Bean("jdbcTemplate")
    public JdbcTemplate jdbcTemplate(@Qualifier("dataSource")DriverManagerDataSource dataSource) {
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        return jdbcTemplate;
    }
}
